#!/bin/bash
#build and install kernel in anolis,centos7,ubuntu
. /opt/hygon/common.sh

os_release=$(eval get_os_release)

case $os_release in
    "anolis")
        yum install -y bc elfutils-libelf-devel.x86_64 openssl-devel.x86_64 make gcc rpm-build flex bison libbpf
    ;;
    "centos")
        yum install -y elfutils-libelf-devel.x86_64 openssl-devel.x86_64  centos-release-scl   centos-release-scl-rh libuuid-devel.x86_64
        yum install -y devtoolset-8.x86_64
        source /opt/rh/devtoolset-8/enable
    ;;
    "ubuntu")
        apt update
        apt-get install -y make gcc flex bison dpkg-dev libelf-dev libssl-dev
             ;;
    "kylin")
        yum install -y elfutils-devel.x86_64 rpm-build.x86_64 make gcc rpm-build flex bison libbpf bc
    ;;
esac

yes "" | make oldconfig
./scripts/config --enable CONFIG_CRYPTO_DEV_CCP
./scripts/config --enable CONFIG_AMD_MEM_ENCRYPT
./scripts/config --enable CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT
./scripts/config --disable CONFIG_HYDCU_FIXUP_HEADER
./scripts/config --enable CONFIG_VFIO_MDEV
./scripts/config --enable CONFIG_VFIO_MDEV_DEVICE
./scripts/config --enable CONFIG_VFIO
./scripts/config --enable CONFIG_VFIO_IOMMU_TYPE1
./scripts/config --enable CONFIG_VFIO_PCI
./scripts/config --enable CONFIG_HYMCCP
./scripts/config -m CONFIG_BLK_DEV_NBD
./scripts/config -m CONFIG_CRYPTO_DEV_HCT
./scripts/config --disable CONFIG_DEBUG_INFO
./scripts/config --enable  CONFIG_CGROUPS
./scripts/config --enable  CONFIG_CGROUP_CPUACCT
./scripts/config --enable  CONFIG_XEN_PV
./scripts/config --enable  CONFIG_HYGON_CSV
./scripts/config --enable  CONFIG_CMA
./scripts/config --enable  CONFIG_HUGETLBFS
./scripts/config --enable  CONFIG_LIBCRC32C
./scripts/config --disable CONFIG_X86_CPU_RESCTRL
./scripts/config --enable CONFIG_VIRTIO_BLK
./scripts/config --enable CONFIG_VIRTIO_NET
./scripts/config --disable CONFIG_IO_STRICT_DEVMEM
./scripts/config --disable SYSTEM_TRUSTED_KEYS
./scripts/config --disable SYSTEM_REVOCATION_KEYS
make olddefconfig

mkdir -p /opt/hygon/kernel
rm -rf /opt/hygon/kernel/*

# The parameters are not set repeatedly for multiple tests
sed -i '
/^GRUB_CMDLINE_LINUX=/ {
  /amd_iommu=on/! s!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 amd_iommu=on"!
  /mem_encrypt=on/ {
    /kvm-amd.sev=1/! s!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 kvm-amd.sev=1"!
    /kvm-amd.sev=1/ {
      /kvm-amd.sev_es=1/! s!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 kvm-amd.sev_es=1"!
    }
  }
  /mem_encrypt=on/! s!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 mem_encrypt=on kvm-amd.sev=1 kvm-amd.sev_es=1"!
}' /etc/default/grub

case $os_release in
    "anolis")
        rm -rf ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
        make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv" rpm-pkg
        rpm -ivh --force --nodeps ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
        grub2-mkconfig -o /boot/efi/EFI/anolis/grub.cfg
        mv ~/rpmbuild/RPMS/x86_64/kernel-*.rpm /opt/hygon/kernel/
    ;;
    "kylin")
        make olddefconfig
        rm -rf ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
        make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv" rpm-pkg
        grub2-mkconfig -o /boot/efi/EFI/kylin/grub.cfg
        mv ~/rpmbuild/RPMS/x86_64/kernel-*.rpm /opt/hygon/kernel/
    ;;
    "centos")
        rm -rf ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
        make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv" rpm-pkg
        rpm -ivh --force --nodeps ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
        grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
        mv ~/rpmbuild/RPMS/x86_64/kernel-*.rpm /opt/hygon/kernel/
    ;;
    "ubuntu")
        ./scripts/config --disable  CONFIG_DEBUG_INFO_BTF
        ./scripts/config --enable CONFIG_EFI
        ./scripts/config --enable CONFIG_EFI_STUB
        make olddefconfig
        rm -f ../linux*-csv*.deb
        make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv" deb-pkg
        dpkg -i ../linux*-csv*.deb
        update-grub
        mv ../linux-*-csv*.deb  /opt/hygon/kernel/
    ;;
    esac

cd /opt/hygon/
tar czvf kernel.tgz kernel
